<?php
class ModuleDocumentAction extends Action{
	public function _initialize(){
		$retVal = Session::init();
		if (!$retVal) {
            Output::instance()->resultCode(code_access_denied);
            exit;
        }

        $this->cid = Session::$user['customerId'];
        Session::switchCustomer(0);
		$this->module_document_db = D('ModuleDocument');
        	$cid = $this->cid;
		Session::switchCustomer($cid);
	}

    /**
     * 获取帮助文档
     * @param $module
     * @param $entry
     */
	public function get_document($module,$entry){
        $map = $result =  array();
        $map['moudle'] = $module;
        $map['entry'] = $entry;
        $map['deleted'] = 0;

        $info = $this->module_document_db->alias('a')
        ->where($map)
        ->find();

        if($info){
         $result['code'] = 1;
         $result['info'] = $info;
        }else{
        	$result['code'] = 0 ;

        	$result['info'] = '不存在对应的在线帮助';
        }
        Output::instance()->object($result);

	}

    /**
     * 获取活动相关文档列表
     * @param string $keyWord 搜索词
     * @param int $start
     * @param string $order
     * @param string $by
     * @param int $limit
     */
    public function getDocumentList($keyWord='', $start=0, $order='updateTime', $by='desc', $limit=10){
        $retVal = Session::init();
        if(!$retVal){
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        $map = array();
        if($keyWord !== ''){
            $map['m.name|d.entryName'] = array(array('like', '%'.$keyWord.'%'), array('like', '%'.$keyWord.'%'),'_multi'=>true);
        }
        $map['d.deleted'] = 0;

        $docList = $this->module_document_db->alias('d')
            ->field('d.*, m.name as moduleName')
            ->join('inner join wx_module_group m on d.module=m.group')
            ->where($map)
            ->order($order.' '.$by)
            ->limit(intval($start), intval($limit))
            ->select();
        /*echo '<pre>';
        var_dump();
        echo '<pre>';exit;*/
        $total = $this->module_document_db->alias('d')
            ->field('d.*, m.name as moduleName')
            ->join('inner join wx_module_group m on d.module=m.group')
            ->where($map)
            ->count();

        if(!$docList){
            Output::instance()->object(array(
               'code'=>-1,
               'massage'=>'未查询到活动相关文档',
               'data'=>'',
               'total'=>0
            ));
            return;
        }else{
            Output::instance()->object(array(
                'code'=>0,
                'massage'=>'查询成功',
                'data'=>$docList,
                'total'=>$total,
            ));
        }
    }

    /**
     * 查询一条文档信息
     * @param $id
     */
    public function getDocumentOne($id){
        $retVal = Session::init();
        if(!$retVal){
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        $documentOne = $this->module_document_db
            ->where(array('id'=>$id,'deleted'=>0))
            ->find();
        if($documentOne){
            Output::instance()->object(array(
                'code'=>0,
                'massage'=>'查询成功',
                'data'=>$documentOne
            ));
        }else{
            Output::instance()->object(array(
                'code'=>-1,
                'massage'=>'未查询到相关文档',
                'data'=>''
            ));
        }
        return;
    }

    /**
     * 更新文档
     * @param $docId
     * @param $html     帮助内容
     * @param $title    帮助主题
     * @param string $entry 入口标记
     * @param $entryName    入口名称
     */
    public function saveDocument($docId, $html, $title='', $entry = '', $entryName=''){
        $retVal = Session::init();
        if(!$retVal){
            Output::instance()->resultCode(code_access_denied);
            return;
        }

        /*if(!($this->isPost())){
            Output::instance()->object(array(
               'code' => -1,
               'massage' => '非法提交,请使用加密提交',
               'data' => '',
            ));
        }*/

        if(!empty($title)){
            $data['title'] = strval($title);
        }

        if(!empty($entry)){
            $data['entry'] = strval($entry);
        }

        if(!empty($entryName)){
            $data['entryName'] = strval($entryName);
        }
        $data['html'] = $html ? $html : '';

        $data['updateTime'] = date('Y-m-d h:i:s');

        $retInt = $this->module_document_db
            ->where(array('id'=>intval($docId)))
            ->save($data);
        /*echo '<pre>';
        var_dump($this->module_document_db->getLastSql());
        echo '<br>'.$retInt;
        echo '<pre>';exit;*/
        if(is_numeric($retInt)){
            Output::instance()->object(array(
               'code' => 0,
               'message' => '更新成功',
               'data' => ''
            ));
        }else{
            Log::write('更新文档失败:'.$this->module_document_db->getLastSql());
            Output::instance()->object(array(
                'code'=>-1,
                'message'=>'更新失败',
                'data'=>'',
            ));
        }
    }

}